import {observable, action} from 'mobx';
import { WeaTools } from 'ecCom';

export class OnlineEditStore {
  @observable pageBreak = false;
  @observable showNum = false;//序号显隐
  @observable numActiveKey = '0';
  @observable showStyle = false;//样式显隐
  @observable styleActiveKey = '0';
  @observable showRevision = false;
  @observable showAccept = false;//接受下拉
  @observable showReject = false; //拒绝下拉
  @observable displayRevision = false;
  @observable newAnnotation = false;
  @observable showDelete = false;//删除下拉显隐
  @observable displayAnnotations = false;
  @observable showToolBar = true;
  @observable showFileList  = false//文件下拉

  @observable toolsAllPage = 1;//toolbar 下一页
  @observable toolsNowPage = 1;//toolbar 下一页
  @observable styleNextPage = false; //样式下一页
  
  @observable loading = false;

  @observable tipShow = false;

  @observable uploading = false;
  @observable fullScreen = false; 
  @observable delModalVisible = false; // 删除弹框
  @observable isDelAll = false;// 是否删除所有批注
  
  @action 
  setMobxState = (params = {}) =>　{
    this.hiddenPopover();
    Object.keys(params).forEach(v => {
      this[v] = params[v];
    });
  }

  @action 
  setState = (params = {}) =>　{
    Object.keys(params).forEach(v => {
      this[v] = params[v];
    });
  }

  @action 
  getNextOne = (params = {}) =>　{
    this.setMobxState(params)
  }

  @action.bound
  changeDisplayAnnotations(bool){
    this.displayAnnotations = bool;
  }

  @action 
  hiddenPopover(){
    this.showNum = false;
    this.showStyle = false;
    this.showAccept = false;
    this.showReject = false;
    this.showDelete = false;
    this.showFileList = false;
  }

  @action.bound hideOnePopover(key){
    this[key] = false;
  }

  @action.bound saveLocalFile(){
    const resultStr = jQuery("#CKIframe")[0].contentWindow.submit();
    let fileName;
    try{
      fileName = parent.parent.wfform.getOdocConfig().docParam['field' + parent.parent.wfform.getOdocConfig().docParam.documentTitleField];
    }catch(e){
      fileName = 'TempFile';
    }
    if(!fileName) fileName = parent.parent.wfform.getOdocConfig().docParam.field-1;

    if(!fileName) fileName = '';
    let params = {resultStr:resultStr,fileName:fileName};
    WeaTools.callApi(`/api/odoc/odocFile/convertHtmlToDocx`, 'POST',params).then(data=>{
      if(window.console)console.log('saveLocalFile',data);
      open(encodeURI(data.filePath));
    })
  }

  @action.bound preview(){
    const resultStr = jQuery("#CKIframe")[0].contentWindow.submit();
      let fileName;
      try{
          fileName = parent.parent.wfform.getOdocConfig().docParam['field' + parent.parent.wfform.getOdocConfig().docParam.documentTitleField];
      }catch(e){
          fileName = 'TempFile';
      }
    if(!fileName) fileName = parent.parent.wfform.getOdocConfig().docParam.field-1;
    if(!fileName) fileName = '';
    let params = {resultStr:resultStr,fileName:fileName};
    WeaTools.callApi(`/api/odoc/odocFile/convertHtmlToDocx`, 'POST',params).then(data=>{
      if(window.console)console.log('preview',data);
      open(data.filePreviewUrl);
    })
  }

  @action.bound odocPrint(){
    const resultStr = jQuery("#CKIframe")[0].contentWindow.submit();
      let fileName;
      try{
          fileName = parent.parent.wfform.getOdocConfig().docParam['field' + parent.parent.wfform.getOdocConfig().docParam.documentTitleField];
      }catch(e){
          fileName = 'TempFile';
      }
    if(!fileName) fileName = parent.parent.wfform.getOdocConfig().docParam.field-1;
    if(!fileName) fileName = '';
    let params = {resultStr:resultStr,fileName:fileName};
    WeaTools.callApi(`/api/odoc/odocFile/convertHtmlToDocx`, 'POST',params).then(data=>{
      if(window.console)console.log('odocPrint',data);
      //open(data.filePreviewUrl);
      document.getElementById('printIframe').src=data.filePreviewUrl;
      document.getElementById('printIframe').onload = () => {
        document.getElementById('printIframe').contentWindow.print();
      }
    })
  }

  @action.bound previewWithTempl(){
    const resultStr = jQuery("#CKIframe")[0].contentWindow.submit();
    const odocConfig = parent.parent.wfform.getOdocConfig();
      let fileName;
      try{
          fileName = parent.parent.wfform.getOdocConfig().docParam['field' + parent.parent.wfform.getOdocConfig().docParam.documentTitleField];
      }catch(e){
          fileName = 'TempFile';
      }
    if(!fileName) fileName = odocConfig.docParam.field-1;
    if(!fileName) fileName = '';
      if(window.console)console.log('saveLocalFile fileName',fileName);
    let showMouldId = odocConfig.docParam.showMouldId;
    let docId = odocConfig.docParam.id;
    let params = {resultStr:resultStr,fileName:fileName,docId:docId,showMouldId:showMouldId};
    WeaTools.callApi(`/api/odoc/odocFile/convertHtmlToDocx`, 'POST',params).then(data=>{
      if(window.console)console.log('previewWithTempl',data);
      open(data.useTempfilePreviewUrl);
    })
  }

  @action.bound getUploadState(status){
    if(status == "uploading"){
      this.uploading = true;
    }else if(status == "uploaded"){
      this.uploading = false;
    }
  }
}

